from pyecharts.charts import Line, Page
import pyecharts.options as opts
from pyecharts.globals import ThemeType
import pandas as pd
import sys
sys.path.append("..//database")
from SqlWizard import *
from CmnFuncs import *

def run_indice_charts():
    '''
    本模块包含南华商品指数，能化指数，黑色指数，农产品指数，有色金属指数，贵金属指数
    设计图表：
    1. 南华商品指数：NHCI.NH
    2. 能化指数： NHECI.NH
    3. 黑色指数：NHFI.NH
    4. 农产品指数：NHAI.NH
    5. 有色金属指数：NHNFI.NH
    6. 贵金属指数：NHPMI.NH
    7. 以上6项指数的收盘价曲线叠加到一张图中
    '''

    nhci = read_sql_indice('index_nhci')
    nheci = read_sql_indice('index_nheci')
    nhfi = read_sql_indice('index_nhfi')
    nhai = read_sql_indice('index_nhai')
    nhnfi = read_sql_indice('index_nhnfi')
    nhpmi = read_sql_indice('index_nhpmi')

    # 1. 南华商品指数：NHCI.NH
    ci = draw_index_k('南华商品指数', nhci, 2)
    # 2. 能化指数： NHECI.NH
    eci = draw_index_k('南华能化指数', nheci, 2)
    # 3. 黑色指数：NHFI.NH
    fi = draw_index_k('南华黑色指数', nhfi, 2)
    # 4. 农产品指数：NHAI.NH
    ai = draw_index_k('南华农产品指数', nhai, 2)
    # 5. 有色金属指数：NHNFI.NH
    nfi = draw_index_k('南华有色金属指数', nhnfi, 2)
    # 6. 贵金属指数：NHPMI.NH
    pmi = draw_index_k('南华贵金属指数', nhpmi, 2)
    # 7. 以上6项指数的收盘价曲线叠加到一张图中
    datelist = (nhci['close'] + nheci['close'] + nhfi['close'] +
                nhai['close'] + nhnfi['close'] + nhpmi['close']).index.to_list()
    cil, ecil, fil, ail, nfil, pmil = [], [], [], [], [], []
    for date in datelist:
        try:
            cila = nhci['close'][date]
        except:
            cila = None
        else:
            pass
        cil.append(cila)

        try:
            ecila = nheci['close'][date]
        except:
            ecila = None
        else:
            pass
        ecil.append(ecila)

        try:
            fila = nhfi['close'][date]
        except:
            fila = None
        else:
            pass
        fil.append(fila)

        try:
            aila = nhai['close'][date]
        except:
            aila = None
        else:
            pass
        ail.append(aila)

        try:
            nfila = nhnfi['close'][date]
        except:
            nfila = None
        else:
            pass
        nfil.append(nfila)

        try:
            pmila = nhpmi['close'][date]
        except:
            pmila = None
        else:
            pass
        pmil.append(pmila)
    # dica = {
    #     '商品指数': cil,
    #     '能化指数': ecil,
    #     '黑色指数': fil,
    # }
    # dicb = {
    #     '农产品指数': ail,
    #     '有色金属指数': nfil,
    #     '贵金属指数': pmil
    # }
    linea = (
        Line(init_opts=opts.InitOpts(theme=ThemeType.CHALK))
        .add_xaxis(datelist)
        .add_yaxis(
            '商品指数', cil
        )
        .add_yaxis(
            '能化指数', ecil
        )
        .add_yaxis(
            '黑色指数', fil
        )
        .add_yaxis(
            '有色金属指数', nfil
        )
        .set_global_opts(yaxis_opts=opts.AxisOpts(is_scale=True), toolbox_opts=opts.ToolboxOpts(),
                         datazoom_opts=opts.DataZoomOpts(range_start=0, range_end=100),
                         tooltip_opts=opts.TooltipOpts(trigger='axis'))
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .extend_axis(yaxis=opts.AxisOpts(is_scale=True))
    )
    lineb = (
        Line()
        .add_xaxis(datelist)
        .add_yaxis(
            '农产品指数（右轴）', ail, yaxis_index=1
        )
        .add_yaxis(
            '贵金属指数（右轴）', pmil, yaxis_index=1
        )
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    )

    linea.overlap(lineb)
    linea.width = '1600px'
    linea.height = '800px'

    page = Page(page_title='南华商品指数', layout=Page.SimplePageLayout)
    page.add(
        ci,
        eci,
        fi,
        ai,
        nfi,
        pmi,
        linea
    )
    page.render('南华商品指数.html')



